package com.shujia.day18;

/*
    创建线程的第二种方式：实现Runnable接口，借助Thread类创建线程对象

    若将来每一个线程执行的逻辑是一样的话，推荐采用第二种实现Runnable接口方式实现多线程
    若将来每一个线程执行逻辑不一样的话，推荐采用第一种继承Thread类方式实现多线程
 */

class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 1; i <= 200; i++) {
            //可以先通过获取当前执行的线程对象，调用内部getName()方法
            System.out.println(Thread.currentThread().getName()+"-"+i);
        }
    }
}

public class RunnableDemo1 {
    public static void main(String[] args) {
        //创建Runnable对象
        MyRunnable r1 = new MyRunnable();
        //创建线程对象
//        Thread t1 = new Thread(r1);
//        Thread t2 = new Thread(r1);
//        Thread t3 = new Thread(r1);
        //创建线程对象同时起名字
        Thread t1 = new Thread(r1, "李刚");
        Thread t2 = new Thread(r1, "祝帅");
        Thread t3 = new Thread(r1, "吴问强");

        //获取线程优先权 默认线程优先级是 5
//        System.out.println("t1: "+t1.getPriority());
//        System.out.println("t2: "+t2.getPriority());
//        System.out.println("t3: "+t3.getPriority());
        t1.setPriority(1); // 1-10
        t1.setPriority(10);
        t1.setPriority(1);

        t1.start();
        t2.start();
        t3.start();
    }
}
